package com.flowersinc.shop.controls
{
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.controls.Image;
	import mx.effects.Glow;

	public class RatingControlBase extends HBox
	{	
		public var glowStar : Glow;
		public var unglowStar : Glow;
		
		[Bindable]
		public var images : Object;
		
		[Bindable]
		protected var imageRepeaterDataProvider : Array = [1, 2, 3, 4, 5];
		
		[Bindable]
		[Embed (source="../images/selectedImage.png")]
		private var selectedImage : Class;
		
		[Bindable]
		[Embed (source="../images/unselectedImage.png")]
		private var unselectedImage : Class;
		
		[Bindable]
		public function get selectedStars() : Number 
		{
			return _selectedStars;
		}
		
		public function set selectedStars(value : Number) : void 
		{
			_selectedStars = value;
			updateSelectedStars(value);
		}
		
		protected function onStarImageClick(event : MouseEvent) : void 
		{	
			selectedStars = Image(event.currentTarget).data as int;
		}
		
		protected function getImageSource(index : int) : Class {
			 return index < selectedStars ? selectedImage : unselectedImage;
		}
		
		protected function onMouseOver(event : MouseEvent) : void 
		{
			glowStar.target = Image(event.currentTarget);
			glowStar.play();
		}
		
		protected function onMouseOut(event : MouseEvent) : void 
		{
			unglowStar.target = Image(event.currentTarget);
			unglowStar.play();
		}
			
		private function updateSelectedStars(index : int) : void 
		{
			if (images != null)
			{
				var imagesArray : Array = images as Array;
				
				for each (var image : Image in imagesArray) 
				{
					if (int(image.data) <= index) 
					{
						image.source = selectedImage;
					}
					else
					{
						image.source = unselectedImage;
					}
				}	
			}	
		}
			
		private var _selectedStars : int = 0;
	}
}